<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace app\shop\controller;

use addon\live\model\Goods;
use addon\sitemessage\model\MemberMessage;
use addon\sitemessage\model\Sitemessage;
use app\model\hotel\Hotel as HotelModel;
use app\model\message\Message;
use app\model\message\Message as MessageModel;
use app\model\shop\Shop as ShopModel;
use app\model\shop\ShopAccount;
use app\model\shop\ShopCategory as ShopCategoryModel;
use app\model\shop\ShopDeposit;
use app\model\shop\ShopGroup as ShopGroupModel;
use app\model\system\Address as AddressModel;
use app\model\order\OrderCommon;
use app\model\web\Config as ConfigModel;

/**
 * 店铺
 * Class Shop
 * @package app\shop\controller
 */
class Shop extends BaseShop
{


    public function __construct()
    {
        //执行父类构造函数
        parent::__construct();

    }

    /**
     * 店铺设置
     * @return mixed
     */
    public function config()
    {
        $shop_model = new ShopModel();
        $condition = array(
            ["site_id", "=", $this->site_id]
        );
        if (request()->isAjax()) {
            $logo = input("logo", '');//店铺logo
            $avatar = input("avatar", '');//店铺头像（大图）
            $banner = input("banner", '');//店铺条幅
            $seo_keywords = input("seo_keywords", '');//店铺关键字
            $seo_description = input("seo_description", '');//店铺简介
            $data = array(
                "logo" => $logo,
                "avatar" => $avatar,
                "banner" => $banner,
                "seo_keywords" => $seo_keywords,
                "seo_description" => $seo_description,
//				'shop_status' => 1
            );
            $res = $shop_model->editShop($data, $condition);
            return $res;
        } else {
            $shop_info_result = $shop_model->getShopInfo($condition);
            $shop_info = $shop_info_result["data"];
            $this->assign("shop_info", $shop_info);
            return $this->fetch("shop/config");
        }

    }

    /**
     * 联系方式
     * @return mixed
     */
    public function contact()
    {
        $shop_model = new ShopModel();
        $condition = array(
            ["site_id", "=", $this->site_id]
        );
        if (request()->isAjax()) {
            $province = input("province", 0);//省级地址
            $province_name = input("province_name", '');//省级地址
            $city = input("city");//市级地址
            $city_name = input("city_name", '');//市级地址
            $district = input("district", 0);//县级地址
            $district_name = input("district_name", '');//县级地址
            //$community = input("community", 0);//乡镇地址
            //$community_name = input("community_name", '');//乡镇地址
            $address = input("address", 0);//详细地址
            $full_address = input("full_address", 0);//完整地址
            $longitude = input("longitude", '');//经度
            $latitude = input("latitude", '');//纬度

            //$qq = input("qq", '');//qq号
            //$ww = input("ww", '');//阿里旺旺
            //$email = input("email", '');//邮箱
            //$telephone = input("telephone", '');//联系电话
            $name = input("name", '');//联系人姓名
            $mobile = input("mobile", '');//联系人手机号

            $work_week = input("work_week", '');//工作日  例如 : 1,2,3,4,5,6,7
            $start_time = input("start_time", 0);//开始时间
            $end_time = input("end_time", 0);//结束时间
            $data = array(
                "province" => $province,
                "province_name" => $province_name,
                "city" => $city,
                "city_name" => $city_name,
                "district" => $district,
                "district_name" => $district_name,
                //"community" => $community,
                //"community_name" => $community_name,
                "address" => $address,
                "full_address" => $full_address,
                "longitude" => $longitude,
                "latitude" => $latitude,
                //"qq" => $qq,
                //"ww" => $ww,
                //"email" => $email,
                //"telephone" => $telephone,
                "work_week" => $work_week,
                "start_time" => $start_time,
                "end_time" => $end_time,
                "name" => $name,
                "mobile" => $mobile,
                'site_name' => input('site_name', ''),//店铺名称
                'category_id' => input('category_id', 0),//分类id
                'category_name' => input('category_name', ''),//分类名称
                'group_id' => input('group_id', 0),//组id
                'group_name' => input('group_name', ''),//组名称
                "avatar" => input('avatar', ''),
                "banner" => input('banner', ''),

                'contacts_card_electronic_2' => input('contacts_card_electronic_2', ''),//申请人身份证正面
                'contacts_card_electronic_3' => input('contacts_card_electronic_3', ''),//申请人身份证反面
                'contacts_card_no' => input('contacts_card_no', ''),
                'contacts_card_name' => input('contacts_card_name', ''),
                'contacts_card_type' => input('contacts_card_type', ''),
                'contacts_card_start_time' => input('contacts_card_start_time', ''),
                'contacts_card_end_time' => input('contacts_card_end_time', ''),

                'settlement_bank_account_name' => input('settlement_bank_account_name', ''),//开户人
                'settlement_bank_account_number' => input('settlement_bank_account_number', ''),//银行卡号
                'settlement_bank_name' => input('settlement_bank_name', ''),//开户行
                'settlement_bank_type' => input('settlement_bank_type', ''),//开户行
                'settlement_bank_img' => input('settlement_bank_img', ''),//开户行

                'business_licence_number' => input('business_licence_number', ''),//统一社会信用码 input
                'business_licence_number_electronic' => input('business_licence_number_electronic', ''),//营业执照电子版

                'license_time_type' => input('license_time_type', 1),
                'license_start_date' => input('license_start_date', ''),
                'license_end_date' => input('license_end_date', ''),
                'shop_status'=>0
            );

            $res = $shop_model->editShop($data, $condition);
            return $res;
        } else {

            $shop_info_result = $shop_model->getShopInfo($condition);
            $shop_info = $shop_info_result["data"];
            $this->assign("info", $shop_info);

            //查询省级数据列表
            $address_model = new AddressModel();
            $list = $address_model->getAreaList([["pid", "=", 0], ["level", "=", 1]]);
            $this->assign("province_list", $list["data"]);

            //获取地址key配置
            $config_model = new ConfigModel();
            $config = $config_model->getMapConfig();
            $this->assign('map_key', $config['data']['value']);

            //商家主营行业
            $shop_category_model = new ShopCategoryModel();
            $shop_category_list = $shop_category_model->getCategoryList([], 'category_id, category_name', 'sort asc');
            $this->assign('shop_category_list', $shop_category_list[ 'data' ]);

            //商家主营行业
            $shop_group_model = new ShopGroupModel();
            $shop_group_list = $shop_group_model->getGroupList([ ], 'group_id,is_own,group_name,fee,remark', 'is_own asc,fee asc');
            $this->assign('shop_group_list', $shop_group_list[ 'data' ]);

            $hotel_model = new HotelModel();
            $id_card_type_list = $hotel_model->idCardType();
            $this->assign('id_card_type_list', $id_card_type_list);

            return $this->fetch("shop/contact");
        }

    }

    /**
     * 店铺装修
     * @return mixed
     */
    public function decoration()
    {
        return $this->fetch("shop/decoration");
    }

    /**
     * 账户信息
     * @return mixed
     */
    public function account()
    {
        $shop_model = new ShopModel();
        $shop_account_model = new ShopAccount();
        //获取商家转账设置
        $shop_withdraw_config = $shop_account_model->getShopWithdrawConfig();

        $condition = array(
            ["site_id", "=", $this->site_id]
        );
        $shop_info_result = $shop_model->getShopInfo($condition, 'account, account_withdraw, shop_baozhrmb');
        $shop_info = $shop_info_result["data"];

        //获取店家结算账户信息
        $shop_cert_result = $shop_model->getShopCert($condition, 'bank_type, settlement_bank_account_name, settlement_bank_account_number, settlement_bank_name, settlement_bank_address');

        $this->assign("account", $shop_info['account']);//账户余额
        $this->assign("account_withdraw", $shop_info['account_withdraw']); //已提现
        $this->assign('order_calc', 0);//待结算
        $this->assign('shop_deposit', $shop_info['shop_baozhrmb']);//保证金
        $this->assign('shop_withdraw_config', $shop_withdraw_config['data']['value']);//商家转账设置
        $this->assign('shop_cert_info', $shop_cert_result['data']);//店家结算账户信息
        return $this->fetch("shop/account");
    }

    /**
     * 认证信息
     */
    public function cert()
    {
        $shop_model = new ShopModel();
        $condition = array(
            ["site_id", "=", $this->site_id]
        );
        $cert_info_result = $shop_model->getShopCert($condition);
        $cert_info = $cert_info_result["data"];
        $this->assign("cert_info", $cert_info);
        return $this->fetch("shop/cert");
    }


    /**
     * 获取待结算列表
     */
    public function getOrderCalc()
    {
        if (request()->isAjax()) {
            $order = new OrderCommon();
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $is_refund = input("is_refund", '');
            $condition = array(
                ['site_id', "=", $this->site_id],
                ['is_settlement', "=", 0],
                ['order_status', "not in", '0,-1'],
            );

            if ($is_refund !== '') {
                $condition[] = ['refund_status', '=', $is_refund];
            }
            $order_no = input('order_no', '');
            if ($order_no) {
                $condition[] = ['order_no', 'like', '%' . $order_no . '%'];
            }

            $list = $order->getOrderPageList($condition, $page, $page_size, 'create_time desc', $field = 'order_id,order_no,order_type_name,order_status_name,order_money,shop_money,platform_money,is_settlement,create_time,order_type');
            return $list;
        }
    }


    /**
     * 获取账户流水
     */
    public function getShopAccount()
    {
        if (request()->isAjax()) {
            $account_model = new ShopAccount();
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $order = input("order", "create_time desc");
            $condition[] = ['site_id', "=", $this->site_id];

            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = ["create_time", ">=", date_to_time($start_time)];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = ["create_time", "<=", date_to_time($end_time)];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = ["create_time", "between", [date_to_time($start_time), date_to_time($end_time)]];
            }
            $type_name = input('type_name', '');
            if ($type_name) {
                $condition[] = ['type_name', '=', $type_name];
            }

            $list = $account_model->getAccountPageList($condition, $page, $page_size, $order);
            return $list;
        }
    }

    /**
     * 获取保证金记录
     */
    public function getShopDeposit()
    {
        if (request()->isAjax()) {
            $shop_deposit_model = new ShopDeposit();
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $order = input("order", "id desc");
            $search_text = input("search_text", "");

            $condition = array(
                ['site_id', "=", $this->site_id],
            );
            if (!empty($search_text)) {
                $condition[] = ['pay_no|pay_account_name', 'like', '%' . $search_text . '%'];
            }
            $list = $shop_deposit_model->getShopDepositPageList($condition, $page, $page_size, $order);
            return $list;
        }
    }

    /**
     * 续签信息
     * @return mixed
     */
    public function reopen()
    {
        return $this->fetch("shop/reopen");
    }

    /**
     * 店铺推广
     * return
     */
    public function shopUrl()
    {
        //获取商品sku_id
        $shop_model = new ShopModel();
        $res = $shop_model->qrcode($this->site_id);
        // dump($res);exit;
        return $res;
    }

}